package com.ys.peony.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.DaimianshenModel;
import com.ys.peony.ui.activity.ContractSortListActivity;
import com.ys.peony.ui.widget.MyClickableTextView;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.PixelUtils;
import com.ys.peony.utils.ToastUtils;
import com.ys.peony.utils.YsUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 59153 on 2018/3/28.
 */

public class DialogContract implements View.OnClickListener {
    private Dialog mDialog;
    private Context mContext;
    private Window mWindow;
    private IdCardDialog.MyClickListener mClickListener;
    private EditText et_check;
    private EditText et_phone;
    private Button bt_cancel;
    private Button bt_sure;
    private ImageView iv_agree;
    private TextView tv_link;
    private MyClickableTextView tv_check;

    private String verifyCode = "";

    private String phone;
    private String loan_id;
    private String mData = "";
    private String mClientName;
    private OnContracyListener mListener = null;
    private CountDownTimer mCountDowTimer = new CountDownTimer(YSConstant.VERIFY_TIME, YSConstant.VERIFY_TIME_INTERVAL) {
        @Override
        public void onTick(long l) {
            tv_check.setMyclickable(false);
            tv_check.setText(l / YSConstant.VERIFY_TIME_INTERVAL + "s后重新获取");
        }

        @Override
        public void onFinish() {
            tv_check.setText("重新获取");
            tv_check.setMyclickable(true);
        }
    };

    public DialogContract(Context context) {
        mContext = context;
        init();
    }

    public DialogContract(Context context, IdCardDialog.MyClickListener listener) {
        mContext = context;
        mClickListener = listener;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_contract_verify, null, false);
        mDialog = new Dialog(mContext, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);
        mWindow = mDialog.getWindow();
        mWindow.setGravity(Gravity.CENTER);
        mWindow.setLayout(YsUtil.getScreenWidth(mContext) - PixelUtils.dp2px(50), mWindow.getAttributes().height);
        et_phone = view.findViewById(R.id.et_phone);
        bt_cancel = view.findViewById(R.id.bt_cancel);
        bt_sure = view.findViewById(R.id.bt_sure);
        iv_agree = view.findViewById(R.id.iv_agree);
        tv_link = view.findViewById(R.id.tv_link);
        tv_check = view.findViewById(R.id.tv_check);
        et_check = view.findViewById(R.id.et_check);

        iv_agree.setOnClickListener(this);
        tv_link.setOnClickListener(this);
        tv_check.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                //mClickListener.onClick(null);
            }
        });
    }

    public void setData(final String data, final DaimianshenModel daimianshenModel, final String phone) {
        this.loan_id = daimianshenModel.getId();
        this.mClientName = daimianshenModel.getClient_name();
        this.mData = data;
        et_phone.setText(phone.trim());
        et_check.setText("");
        iv_agree.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_agree_before));
        tv_link.setVisibility(View.VISIBLE);
        show();
    }

    public void setData(final String loan_id, final String phone) {
        this.loan_id = loan_id;
        et_phone.setText(phone.trim());
        tv_link.setVisibility(View.GONE);
        show();
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View view) {
        Drawable.ConstantState t1 = iv_agree.getDrawable().getCurrent().getConstantState();
        //找到需要比较的图片ConstantState类对象
        Drawable.ConstantState t2 = mContext.getResources().getDrawable(R.mipmap.contract_agree_after).getConstantState();
        int isagree = t1.equals(t2) ? 0 : 1;
        switch (view.getId()) {
            case R.id.iv_agree:
                if (isagree == 0) {
                    iv_agree.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_agree_before));
                } else {
                    iv_agree.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_agree_after));
                }
                break;
            case R.id.tv_link:
                Bundle bundle = new Bundle();
                bundle.putString(YSConstant.PARAM, loan_id);
                bundle.putString("data", mData);
                bundle.putString("name", mClientName);
                ((BaseActivity) mContext).skipToTargetPage(ContractSortListActivity.class, bundle);
                dismiss();
                break;
            case R.id.tv_check:
                if (isagree == 1) {
                    ToastUtils.showToast("请同意合同声明");
                    return;
                }
                verifyCode = YsUtil.requestVerifyCode(et_phone.getText().toString().trim());
                mCountDowTimer.start();
                break;
            case R.id.bt_sure:
                if (checkData()) {
                    doRequest();
                }
                break;
            case R.id.bt_cancel:
                dismiss();
                break;
        }
    }

    private boolean checkData() {
        Drawable.ConstantState t1 = iv_agree.getDrawable().getCurrent().getConstantState();
        //找到需要比较的图片ConstantState类对象
        Drawable.ConstantState t2 = mContext.getResources().getDrawable(R.mipmap.contract_agree_after).getConstantState();
        int isagree = t1.equals(t2) ? 0 : 1;
        if (isagree == 1) {
            ToastUtils.showToast("请同意合同声明");
            return false;
        } else if (TextUtils.isEmpty(et_check.getText().toString().trim())) {
            ToastUtils.showToast("请输入验证码");
            return false;
        } else if (!et_check.getText().toString().trim().equals(verifyCode)) {
            ToastUtils.showToast("验证码错误,请重新输入");
            return false;
        } else {
            return true;
        }
    }

    private void doRequest() {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<>();
        params.put("loan_id", loan_id);
        LoadingProgressUtils.showProgressDialog(mContext);
        httpManagerUtils.get(HttpURL.contractSign, params, new BaseRespCallBack() {
            @Override
            public void onRespSuccess(String respStr) {
                ToastUtils.showToast("签约成功");
                dismiss();
                if (mListener != null) {
                    mListener.success();
                }
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                ToastUtils.showToast("签约失败");
            }
        });
    }

    public void setListener(OnContracyListener OnContracyListener) {
        mListener = OnContracyListener;

    }

    public interface OnContracyListener {
        void success();
    }
}
